package com.atguigu.edubanners.service.impl;

import com.atguigu.edubanners.entity.FrontBanner;
import com.atguigu.edubanners.entity.vo.BannerRequestVo;
import com.atguigu.edubanners.mapper.FrontBannerMapper;
import com.atguigu.edubanners.service.FrontBannerService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhuanglijiao
 * @since 2021-10-19
 */
@Service
public class FrontBannerServiceImpl extends ServiceImpl<FrontBannerMapper, FrontBanner> implements FrontBannerService {

    @Autowired
    private FrontBannerMapper bannerMapper;

    //1.查询所有banner
    @Override
    public List<FrontBanner> getAllFrontBanner() {
        List<FrontBanner> list = bannerMapper.selectList(null);
        return list;
    }

    //2.前台轮播图列表
    @Override
    public List<FrontBanner> getFrontAllBannerLimit5() {
        LambdaQueryWrapper<FrontBanner> wrapper = new LambdaQueryWrapper<>();
        wrapper.ne(FrontBanner::getIsDisable, "1");
        wrapper.orderByAsc(FrontBanner::getSort);
        //last方法，拼接sql语句
        wrapper.last("limit 5");
        List<FrontBanner> list = bannerMapper.selectList(wrapper);
        return list;
    }

    //3.修改轮播图排序
    @Override
    public Boolean ModifyFrontBannerSort(BannerRequestVo.ModifyFrontBannerSort modifyFrontBannerSort) {
        String id = modifyFrontBannerSort.getId();//id
        String sort = modifyFrontBannerSort.getSort();//排序

        FrontBanner frontBanner = bannerMapper.selectById(id);
        if (frontBanner == null) {
            throw new GuliException(20001, "轮播图找不到");
        }

        frontBanner.setSort(sort);
        return bannerMapper.updateById(frontBanner) > 0;
    }

}
